﻿Imports System.ComponentModel

Public Class SortableBindingList(Of T)
    Inherits BindingList(Of T)

    Private _sortableList As List(Of T)

    Public Sub New(ByVal list As List(Of T))
        MyBase.New(list)
        _sortableList = list
    End Sub

    Protected Overloads Overrides ReadOnly Property SupportsSortingCore() As Boolean
        Get
            Return True
        End Get
    End Property

    Protected Overloads Overrides Sub ApplySortCore(ByVal prop As PropertyDescriptor, ByVal direction As ListSortDirection)
        Dim sortExpression As String = prop.Name

        If direction = ListSortDirection.Descending Then
            sortExpression = sortExpression + " DESC"
        End If

        Dim comparer As New Phydeaux.Utilities.DynamicComparer(Of T)(sortExpression)

        _sortableList.Sort(comparer.Comparer)
    End Sub

    Protected Overloads Overrides Sub RemoveSortCore()
    End Sub

End Class
